import numpy as np
from 数学建模国赛算法准备.遗传算法.快递公司送货策略 import calculateMat

_mat = calculateMat.calculate()
def read_data():
    # 城市名数组
    city_name = []
    # 城市坐标数组
    city_condition = []
    # 通过open的方法打开txt文件输入数据
    with open('data.txt', 'r', encoding='utf-8') as f:
        lines = f.readlines()  # 读取每一行数据
        # 处理每一行数据
        for line in range(len(lines)):
            lin = lines[line].split('\n')[0]
            # 数据通过空格和逗号分离出坐标x和坐标y以及城市名称
            lin = lin.split(',')
            # print(line)
            # 将分离出的城市名和城市地址放入数组中
            city_name.append(lin[0])
            city_condition.append([float(lin[1]), float(lin[2])])
    # 因为格式原因而转换成numpy数组
    city_condition = np.array(city_condition)
    # 返回处理后的结果
    return city_name, city_condition


point_height = []

with open('height.txt', 'r', encoding='utf-8') as f:
    lines = f.readlines()  # 读取每一行数据
    # 处理每一行数据
    for line in lines:
        line = line.split('\n')[0]
        point_height.append(float(line))

name, condition = read_data()

label = [int(i) for i in name]
# label = [0,28,29,30]



class Solution:
    def subsets(self, nums):
        def backtrack(nums, path, start, height):
            # 将path添加到res结果中

            if height > 25 and path[0] == 0:
                temp = path.copy()[0:-1]
                distance = 0
                for y in range(len(temp) - 1):
                    distance += _mat[temp[y]][temp[y + 1]]
                time = 0.167 * (len(temp) - 1) + (distance+_mat[0, temp[y+1]]) / 25
                if time < 6:
                    if temp not in res:
                        res.append(temp)
            elif height <= 25 and len(path)>=3 and path[0]==0:
                temp = path.copy()
                distance = 0
                for y in range(len(temp) - 1):
                    distance += _mat[temp[y]][temp[y + 1]]
                time = 0.167 * (len(temp) - 1) + (distance + _mat[0, temp[y + 1]]) / 25
                if time < 6:
                    if temp not in res:
                        res.append(temp)
                for i in range(start, len(nums)):
                    # print(i)
                    # 做选择
                    path.append(nums[i])
                    # height = height+point_height[nums[i]]
                    # if height < 25:
                    backtrack(nums, path, i + 1, height + point_height[nums[i]])
                    # 撤销选择
                    # height = height - point_height[nums[i]]
                    path.pop()
            # 当前能够选择的参数列表
            else:
                for i in range(start, len(nums)):
                    # print(i)
                    # 做选择
                    path.append(nums[i])
                    # height = height+point_height[nums[i]]
                    # if height < 25:
                    backtrack(nums, path, i + 1, height + point_height[nums[i]])
                    # 撤销选择
                    # height = height - point_height[nums[i]]
                    path.pop()
        res = []
        backtrack(nums, [], 0, 0)
        return res



solution=Solution()
ans = solution.subsets(label)
print(ans)
res = np.array(ans)
